package com.zgt.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zgt.dao.StudentDao;
import com.zgt.dao.TeacherDao;
import com.zgt.dao.TopicDao;
import com.zgt.model.Student;
import com.zgt.model.Teacher;
import com.zgt.model.Topic;
import com.zgt.service.StudentService;
import com.zgt.util.MD5;
import com.zgt.util.Page;

@Service("studentService")
public class StudentServiceImpl implements StudentService {

	private StudentDao studentDao;
	private TopicDao topicDao;
	private TeacherDao teacherDao;
	private MD5 md5 = new MD5();

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	public TopicDao getTopicDao() {
		return topicDao;
	}
	@Resource
	public void setTopicDao(TopicDao topicDao) {
		this.topicDao = topicDao;
	}

	@Resource
	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	@Resource
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public void deleteStudentById(int id) {
		studentDao.deleteStudentById(id);
	}

	public Student getStudentById(int id) {
		return studentDao.getStudentById(id);
	}

	public Page listStudent(Page page) {
		page.setAllRow(studentDao.getCount());

		List students = studentDao.listStudent(page.getOffset(), page
				.getPageSize());
		page.setPageResult(students);

		return page;
	}

	public Student checkLogin(Student student) {
		return studentDao.checkLogin(student);
	}

	public void addStudent(Student student) {
		studentDao.saveStudent(student);
	}

	public void updateStudent(Student student) {
		studentDao.updateStudent(student);
	}

	@Transactional
	public void selectTeacher(Student student, Teacher teacher) {
		studentDao.updateStudent(student);
		teacher = teacherDao.getTeacherById(teacher.getId());
		teacher.setStudentCount(teacher.getStudentCount() - 1);
		teacherDao.updateTeacher(teacher);
	}

	@Transactional
	public void reSelectTeacher(Student student, Teacher teacher) {
		studentDao.updateStudent(student);
		teacher = teacherDao.getTeacherById(teacher.getId());
		teacher.setStudentCount(teacher.getStudentCount() + 1);
		teacherDao.updateTeacher(teacher);
	}
	
	@Transactional
	public void selectTopic(Student student, Topic topic) {
		studentDao.updateStudent(student);
		topic = topicDao.getTopicById(topic.getId());
		topic.setSelected(topic.getSelected() + 1);
		topicDao.updateTopic(topic);
	}
	
	@Transactional
	public void reSelectTopic(Student student, Topic topic) {
		studentDao.updateStudent(student);
		topic = topicDao.getTopicById(topic.getId());
		topic.setSelected(topic.getSelected() - 1);
		topicDao.updateTopic(topic);
	}

	public void addStudents(List temList) {
		List list = new ArrayList<Student>();
		Iterator li = temList.iterator();
		while (li.hasNext()) {
			Student stu = new Student();

			stu.setStudentNum((String) li.next());
			stu.setPassword(md5.getMD5ofStr((String) li.next()));
			stu.setName((String) li.next());
			stu.setSex((String) li.next());
			stu.setMajor((String) li.next());
			stu.setClassName((String) li.next());
			stu.setTelphone((String) li.next());
			stu.setQq((String) li.next());
			stu.setEmail((String) li.next());

			if ((!stu.getStudentNum().isEmpty() && !stu.getPassword().isEmpty())
					&& (stu.getStudentNum() != "" && stu.getStudentNum() != null)
					&& (stu.getPassword() != "" && stu.getPassword() != null)) {
				list.add(stu);
			}

		}
		studentDao.saveStudents(list);
	}

	public Page listSearch(Page page, String key, String keyType) {
		String query = "from Student where " + keyType + " like '%" + key
				+ "%'";

		page.setAllRow(studentDao.getCount(query.toString()));
		List teachers = studentDao.listSearch(query.toString(), page
				.getOffset(), page.getPageSize());
		page.setPageResult(teachers);

		return page;
	}

	public List<Student> toExcle(String key, String keyType) {
		return studentDao.toExcle(keyType, key);
	}

	public Page listSelect(Page page) {
		String query = "from Student where topic_id <>''";

		page.setAllRow(studentDao.getCount(query.toString()));
		List teachers = studentDao.listSearch(query.toString(), page
				.getOffset(), page.getPageSize());
		page.setPageResult(teachers);

		return page;
	}

	public  List<Student> selectToExcle(String keyType) {
		String query = "from Student t order by t."+ keyType;
		int count = studentDao.getCount(query);
		return studentDao.query(query);
	}

}
